local rri = {}

local Button_OnClick = function (self)
	local parent = self:GetParent()
	local unit = parent.unit
	if UnitExists(unit) then
		SetRaidTarget(unit, self.index)
	end
	parent:Hide()
end

local Button_OnEnter = function (self)
	self.texture:SetAlpha(1)
	self.texture:SetWidth(36)
	self.texture:SetHeight(36)
end

local Button_OnLeave = function (self)
	self.texture:SetAlpha(0.5)
	self.texture:SetWidth(24)
	self.texture:SetHeight(24)
end


function rri:GetFrame()
	local frame = CreateFrame("Frame", nil, UIParent)
	for i = 0, 8 do
		local b = CreateFrame("Button", nil, frame)
		b.index = i
		b:SetScript("OnClick", Button_OnClick)
		b:SetScript("OnEnter", Button_OnEnter)
		b:SetScript("OnLeave", Button_OnLeave)
		local t = b:CreateTexture(nil, "ARTWORK")
		b.texture = t
		t:SetAlpha(0.5)
		t:SetPoint("CENTER")
		t:SetWidth(24)
		t:SetHeight(24)
		if i >= 1 then
			t:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
			SetRaidTargetIconTexture(t, i)
			local radians = (0.375 - i/8) * 360
			b:SetPoint("CENTER", 36*cos(radians), 36*sin(radians))
		else
			t:SetTexture("Interface\\Buttons\\UI-GroupLoot-Pass-Up")
			b:SetPoint("CENTER")
		end
		b:SetWidth(36)
		b:SetHeight(36)
	end
	
	frame:SetWidth(200)
	frame:SetHeight(200)
	frame.unit = "target"

	self.frame = frame
	function self:GetFrame() return self.frame end
	return frame
end

function rri:Show()
	local frame = self:GetFrame()
	frame:ClearAllPoints()
	local x, y = GetCursorPosition()
	local scale = UIParent:GetEffectiveScale()
	frame:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x / scale, y / scale)
	frame:Show()
end

function rri:Hide()
	self:GetFrame():Hide()
end

RadialRaidIcons = rri

-- /run RadialRaidIcons:Show()